package com.ayu.scalalearn.chapter05

/**
  * @Author 徐林玉
  * @Description Test04MyWhile
  * @Date 2022/7/28 10:29
  * @Version 1.0
  */
object Test04MyWhile {

  def main(args: Array[String]): Unit = {


    //1、用闭包实现
    def myWhile(condition: => Boolean): (=> Unit) => Unit = {
      def doLoop(op: => Unit): Unit = {
        if (condition) {
          op
          myWhile(condition)(op)
        }
      }

      doLoop
    }

    var a = 10
    myWhile(a >= 1) {

      println(a)
      a -= 1
    }


    //2、用匿名函数实现
    def myWhile2(a: => Boolean): (=> Unit) => Unit = {
      //op是一个代码块作为参数
      op => {
        if (a) {

          op
          myWhile2(a)(op)
        }

      }
    }

    println("=======================")

    a = 10
    myWhile2(a >= 1) {

      println(a)
      a -= 1
    }


    //函数柯里化
    def myWhile3(a: =>Boolean)(op: =>Unit):Unit={
      if(a){
        op
        myWhile3(a)(op)
      }

    }

    println("=======================")
    a = 10
    myWhile3(a >= 1) {

      println(a)
      a -= 1
    }

  }

}
